<?php
	defined('IN_TS') or die('Access Denied.');
	
	$userid = aac('user')->isLogin();
	
	
	switch($ts){
		//基本信息
		case "base":
			$taskid = intval($_GET['taskid']);
			$strTask = $new['task']->find('task',array(
				'taskid'=>$taskid,
			));

			if ($strTask ['addtime'] < strtotime('-7 day') && $TS_USER ['isadmin'] == 0) {
				tsNotice('超过一周，不允许编辑，如需代为编辑请给微澜图书馆公众号后台留言');
			}

			$strTask['content'] = tsDecode($strTask['content']);
			$arrGroup = $new['task']->findAll('group','isplace=2','groupid asc');
			if($strTask['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
				tsNotice('非法操作！');
			}
			
			$title = '编辑任务信息';
			include template("edit_base");
			break;
		
		case "basedo":

			$taskid = intval($_POST['taskid']);
            $strTask = $new['task']->find('task',array(
                'taskid'=>$taskid,
            ));
            if($strTask['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
                tsNotice('非法操作！');
            }

			$title = trim($_POST['title']);
			$typeid = intval($_POST['typeid']);
			$limittime = $_POST['limittime'];
			$quota =  intval($_POST['quota']);
			$timelong =  intval($_POST['timelong']);
			$content = tsClean($_POST['content']);
			$limittime = strtotime($limittime);



			if($title == '' || $content == ''){
				tsNotice('标题和内容不能为空');
			}

			//更新数据
			$new['task']->update('task',array(
				'taskid'=>$taskid,
			),array(
				'title'	=> $title,
				'typeid' => $typeid,
				'content' => $content,
				'limittime'=>$limittime,
				'timelong'=>$timelong,
				'quota'=>$quota,
				'isaudit'=>$isaudit,
			));
			

			
			header("Location: ".tsUrl('task','show',array('id'=>$taskid)));
		
			break;
			
		case "join":
			
			$taskid = intval($_GET['taskid']);
			$strTask = $new['task']->find('task',array(
				'taskid'=>$taskid,
			));

            if($strTask['userid']!=$userid && $GLOBALS['TS_USER']['isadmin']==0){
                tsNotice('非法操作！');
            }
			
			$arrUser = $new['task']->findAll('task_users',array(
				'taskid'=>$taskid,
			),'addtime desc');

			foreach($arrUser as $k => $item){
				$strUser = aac('user')->find('user_info',array('userid'=>$item['userid']));
				$arrUser[$k]['username']=$strUser['username'];
				$arrUser[$k]['phone']=$strUser['phone'];
			}

			
			$title = '认领信息';
			include template('edit_join');
			
			break;

	}